//=============================================================================
#include "AppStateLobby.h"
#include "AppStateManager.h"
#include "AppStateMenu.h"
#include <sstream>
//=============================================================================
CAppStateLobby CAppStateLobby::Instance;

//=============================================================================
CAppStateLobby::CAppStateLobby() {
}

//=============================================================================
void CAppStateLobby::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
	switch(sym) {
		case SDLK_a: {
			break;
		}
		default: {
		}
	}
}

//------------------------------------------------------------------------------
void CAppStateLobby::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) {
	switch(sym) {
		case SDLK_RETURN: {
			CAppStateManager::SetActiveAppState(APPSTATE_GAME);
			break;
		}
		default: 
		{
		}
	}
}

//=============================================================================
void CAppStateLobby::OnActivate() {
	lobbyBG = CSurface::OnLoad("./images/lobby/lobbyBG.png");

	if(lobbyBG == NULL)
	{
		return;
	}

	//Font initialisation
	if( TTF_Init() == -1 )
    {
        return;    
    }
	font = NULL;
	font = TTF_OpenFont("FreeSans.ttf", 32);
	if (font == NULL)
	{
		return;
	}

    if(!CClient::getInstance().Connect())
	{
		CAppStateManager::SetActiveAppState(APPSTATE_MENU);
		return;
	}
	
}

//-----------------------------------------------------------------------------
void CAppStateLobby::OnDeactivate() {
	if(lobbyBG) {
        SDL_FreeSurface(lobbyBG);
	    lobbyBG = NULL;
    }
}

//-----------------------------------------------------------------------------
void CAppStateLobby::OnLoop() {
	CClient::getInstance().RunLobby();
}

//-----------------------------------------------------------------------------
void CAppStateLobby::OnRender(SDL_Surface* Surf_Display) {

	SDL_BlitSurface(lobbyBG, NULL, Surf_Display, NULL);
		SDL_Surface* text;
		text = NULL;
		SDL_Rect TextPos;
		TextPos.x = WWIDTH/2 - 40;
		TextPos.y = WHEIGHT/2;
		SDL_Colour black = {0,0,0};
		int i = CEntity::PlayerList.size();
		int x = 480;
		int y = 240;
		for(int p=0 ; p < i; p++)
		{
			if(p > 0)
			{
				y+= 30;
			}
			SDL_Surface* Text_Solid;
			Text_Solid = NULL;
			SDL_Rect TextPos;
			TextPos.x = x;
			TextPos.y = y;
			std::string myName = CEntity::PlayerList.at(p)->name;
			
			SDL_Colour black = {0,0,0};
			Text_Solid = TTF_RenderText_Blended(font,myName.c_str(), black);
			SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &TextPos);
		
		
		}
		std::stringstream st;
		st << i;
		std::string mystring = "Players: " + st.str();
		text = TTF_RenderText_Blended(font, mystring.c_str(), black);
		SDL_BlitSurface(text, NULL, Surf_Display, &TextPos);
		SDL_FreeSurface(text);
	
}

//=============================================================================
CAppStateLobby* CAppStateLobby::GetInstance() {
	return &Instance;
}

//=============================================================================
